package com.portal.cwegportal.service;

import com.appcenter.jz.app.CurrentUser;
import com.appcenter.jz.page.PagedResult;
import com.appcenter.jz.unit.ObjectUnit;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.portal.cwegportal.customenum.FileBusinessType;
import com.portal.cwegportal.input.friendshiplink.*;
import com.portal.cwegportal.mapper.FriendshipLinkMapper;
import com.portal.cwegportal.model.FriendshipLink;
import com.portal.cwegportal.output.friendshiplink.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
/**
 *  创建人:王增立
 *  备注: 友情链接
 *  时间: 2019/4/26 15:00
 *  参数: 
 */
@Service
public class FriendshipLinkService {
    @Autowired
    FriendshipLinkMapper friendshipLinkMapper;
    @Autowired
    FileService fileService;

    @Transactional
    /**
     *  创建人:王增立
     *  备注: 创建友情链接
     *  时间: 2019/4/26 15:00
     *  参数: 
     */
    public CreateFriendshipLinkOutput createFriendshipLink(CreateFriendshipLinkInput input) throws Exception {
        CreateFriendshipLinkOutput output = new CreateFriendshipLinkOutput();
        FriendshipLink FriendshipLink=new FriendshipLink();
        ObjectUnit.Copy(input.getItem(),FriendshipLink);
        FriendshipLink.setGmtCreate(new Date());
        FriendshipLink.setGmtModified(new Date());
        FriendshipLink.setDeleted(false);

        FriendshipLink.setCreatorCode(CurrentUser.getUserCode());
        FriendshipLink.setCreatorId(CurrentUser.getUserId());
        FriendshipLink.setCreatorName(CurrentUser.getUserName());

        friendshipLinkMapper.insertSelective(FriendshipLink);
        fileService.saveFiles(input.getItem().getFiles(),FriendshipLink.getId().toString(),FileBusinessType.FriendshipLink.toString());
        output.setItem(FriendshipLink);
        return output;
    }
/**
 *  创建人:王增立
 *  备注: 修改友情链接
 *  时间: 2019/4/26 15:00
 *  参数: 
 */
    @Transactional
    public EditFriendshipLinkOutput editFriendshipLink (EditFriendshipLinkInput input) throws Exception{
        EditFriendshipLinkOutput output=new EditFriendshipLinkOutput();
        FriendshipLink oldEntity= friendshipLinkMapper.selectByPrimaryKey(input.getItem().getId());
        oldEntity.setLinkName(input.getItem().getLinkName());
        oldEntity.setLinkDesc(input.getItem().getLinkDesc());
        oldEntity.setLinkType(input.getItem().getLinkType());
       oldEntity.setSortNo(input.getItem().getSortNo());
       oldEntity.setLinkUrl(input.getItem().getLinkUrl());
       oldEntity.setGmtModified(new Date());
        friendshipLinkMapper.updateByPrimaryKey(oldEntity);
        fileService.saveFiles(input.getItem().getFiles(),oldEntity.getId().toString(),FileBusinessType.FriendshipLink.toString());
        output.setItem(oldEntity);
        return  output;
    }

/**
 *  创建人:王增立
 *  备注: 删除友情链接
 *  时间: 2019/4/26 15:01
 *  参数: 
 */
    public DeleteFriendshipLinkOutput deleteFriendshipLink(DeleteFriendshipLinkInput input) {
        DeleteFriendshipLinkOutput output = new DeleteFriendshipLinkOutput();
        FriendshipLink oldEntity = friendshipLinkMapper.selectByPrimaryKey(input.getItem().getId());
        oldEntity.setDeleted(true);
        friendshipLinkMapper.updateByPrimaryKey(oldEntity);
        output.setItem(oldEntity);
        return output;
    }
/**
 *  创建人:王增立
 *  备注: 获取友情链接
 *  时间: 2019/4/26 15:01
 *  参数: 
 */
    public GetFriendshipLinksOutput getFriendshipLinks(GetFriendshipLinksInput input) {
        GetFriendshipLinksOutput output = new GetFriendshipLinksOutput();

        //logger.error("log4j2 error!");

        PageHelper.startPage(input.getPage().getPageNo(),input.getPage().getPageSize(),"sort_no");
        //List<Menus> ls = MenuMapper.SearchMenus(input);
        Example example = new Example(FriendshipLink.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("deleted",0);
        List<FriendshipLink> ls=friendshipLinkMapper.selectByExample(example);
        PageInfo<FriendshipLink> page = new PageInfo<>(ls);
        List<FriendshipLink> resultLs=page.getList();
        for(FriendshipLink item:resultLs){
            item.setFiles(fileService.getFiles(item.getId().toString(), FileBusinessType.FriendshipLink.toString()));
        }
        output.setItems(new PagedResult<FriendshipLink>(resultLs,(int)page.getTotal()));
        return  output;

    }
/**
 *  创建人:王增立
 *  备注: 加载友情链接
 *  时间: 2019/4/26 15:28
 *  参数: 
 */
    public PageLoadOutput pageLoad(PageLoadInput input) {
        PageLoadOutput output = new PageLoadOutput();
        output.setItems(getFriendshipLinks(input).getItems());
        return output;
    }
}
